package scripts;
import bot.ScriptMethods;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;

public class lowWall extends ScriptMethods implements Runnable {

	public Thread scriptThread;
	public boolean scriptRunning = false;

	public void start() {
		System.gc();
		getAccountSettings();

		scriptRunning = true;
		scriptThread = new Thread(this);
		scriptThread.start();
	}

	public void stop() {
		System.gc();
		scriptRunning = false;
	}

	public void run() {
		

		int[] startExp = setStartExp();
		long start = System.currentTimeMillis();
	    
		/*
		 * UI START
		 */
		JFrame frame = new JFrame("EXP change");
		frame.setSize(500, 500);
		frame.setVisible(true);
		frame.setLayout(null);		
		JLabel timeRunning = new JLabel("Running for: " );
		JLabel hitLabel = new JLabel("Agility Exp Gained: ");
		timeRunning.setBounds(15, 40, 300, 15);
		hitLabel.setBounds(15, 10, 200, 15);
		frame.add(timeRunning);
		frame.add(hitLabel);
		/*
		 * UI END
		 */
		
		while (scriptRunning) {
			try {
				
				long elapsedTimeMillis = (System.currentTimeMillis()-start);
		        String timeRunningMillis = String.format("Running For: %d hour(s), %d minute(s), and %d second(s)", elapsedTimeMillis/(1000*60*60), (elapsedTimeMillis%(1000*60*60))/(1000*60), ((elapsedTimeMillis%(1000*60*60))%(1000*60))/1000);
					hitLabel.setText("Agility Exp Gained: " + expChange("Agility", startExp));
					timeRunning.setText(timeRunningMillis);
					
					
					
				handleEvents();
				int[] lowWall = getWallById(163);
				if (lowWall[0] != -1 && !isSleeping()) {
					atWall(lowWall[1], lowWall[2]);
					wait(random(600, 700));
				} 
				wait(600);
				wait(random(50, 100));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public void handleEvents() throws Exception {
		if (welcomeBoxOpen())
			setWelcomeBox();
		if (!isLoggedIn())
			doLogin();
		if (getFatigue() > 80 && !isSleeping()) {
			wait(600);
			useItem(1263);
			wait(600);
		}
		if (getFatigueSleeping() < 10 && isSleeping())
			doSleeping();
		if (getFightMode() != fightMode)
			setFightMode();
	}
	
	public int expChange(String skill, int[] startExp){
		int[] currentExp = new int[17];
		int[] expDifference = new int[17];
		int val = -1;
		String[] statArray = {"Attack", "Defense", "Strength", "Hits", "Ranged", "Prayer", "Magic", "Cooking", "Woodcut", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining", "Herblaw", "Agility"};
		

		for(int i = 0; i < 17; i++){
			if(statArray[i] == skill){
				return ((getCurrentExperience()[i] - startExp[i]) / 4);
			}
		}
		debug("You need to reference a skill by name!");
		debug("Right: Defense");
		debug("Wrong: defenSe, or, [1]");
		return -1;
		}
	
	public int[] setStartExp(){
		int[] startExp = new int[17];
		for(int i = 0; i < 17; i++){
			  startExp[i] = getCurrentExperience()[i];
			  wait(50);
		  }
		return startExp;
		
	}
}